Skip to content

Conversation

manerlan
Copy link
Member

Closes #65.

Reviews now belong to the logged-in user instead of an anonymous ID stored in the browser. Anyone could create unlimited anonymous IDs before, so this also stops one person from leaving many reviews. It is the first part of the profile work: User.reviews is ready for the «Mine anmeldelser» section of the profile page.

What

Database

  • Migration: review.user_id (FK to app_user, ON DELETE CASCADE), one review per user per wine, and exactly one author per review (account or old anonymous student). An index for a user's reviews, newest first.
  • Old anonymous reviews are kept, so the averages on the VM do not change.

API

  • saveReview, deleteReview and myReview use the user from the session (auth.requireUser() / currentUser()). The userId arguments are removed: the author is never taken from the request.
  • author is the username, or null for old anonymous reviews (the frontend shows «Anonym student»). Usernames are loaded with a DataLoader; the password hash is never read.
  • New: User.reviews(first, after) with the same pages as the wine search. Reviews are public, so it also works for other users' profiles later.
  • toConnection helper shared by the wine search and User.reviews.
  • backend/vitest.config.ts: tests never use the database in backend/.env.

Frontend

  • The review form is only shown when logged in. Visitors see the reviews and «Logg inn for å gi din dom», which brings them back to the wine after login.
  • The cache starts over after login, like after logout, so nothing fetched as a visitor (such as myReview) is shown afterwards.
  • useUserId is removed.
  • Auth form: Norwegian messages next to the field they are about (noValidate, aria-invalid, aria-describedby), focus on the first error, capital letters allowed in the username (sent in lowercase), and aria-disabled instead of disabled so the button keeps focus. API errors about a field are shown at that field; the others with role="alert" and the shared texts from lib/errors.ts.

E2E

  • e2e/support/graphql.ts: one GraphQL stub for all specs. Me answers «not logged in» by default, and an operation without a stub fails instead of getting another operation's data.
  • The landing test checks that all content comes from one Landing query, plus the shared Me. New tests for «Logg inn» and the username in the menu.

Docs

  • docs/api.md: reviews with login, User.reviews, error codes, database tests.
  • docs/manual-testing.md: how to test login, reviews and other user data by hand, with a checklist for the coming lists.
  • README: status, features, plan and known issues.

main (error handling PR) was merged into this branch. Conflicts in useWineReviews.ts and StudentReviews.tsx were resolved by keeping both changes: the new { ok, error } result and the removed userId.

How it was tested

  • npm run check from the root
  • Backend tests with TEST_DATABASE_URL against a separate test database (no skipped tests): login required, users cannot see or delete each other's reviews, the average follows at once, old anonymous reviews, User.reviews with pages and an invalid cursor
  • npm run e2e: all pass except filters.spec.ts › filtrer, sorter, last på nytt, angre og nullstill on mobile. It fails on main as well, without this PR (#NN)
  • Migration up, down and up again, and the constraints checked in psql
  • By hand, following docs/manual-testing.md sections 1–5: register and login errors, staying logged in, reviews with two users in two windows, the API with curl, keyboard only
  • SearchPage.test.tsx › sender ett søk etter at brukeren har skrevet ferdig sometimes fails when the whole suite runs, also on main. It passes on its own (timing; separate issue)

How to review

The commits are small and build on each other, so commit by commit is easiest:

  1. feat(db): migration
  2. feat(api): reviews as the logged-in user, with API tests
  3. feat(reviews): the interface
  4. feat(api): User.reviews
  5. Merge of main
  6. fix(auth): the auth form
  7. test(e2e): shared GraphQL stub
  8. docs

To try it: npm run db:migrate in backend/, then section 3 of docs/manual-testing.md.

Not in this PR

  • Favourites, «Vil prøve» and «Har prøvd» in the database, and the profile page (next PRs).
  • Removing the old student table, once nothing reads it.

AI

Code, tests, docs, and pr-text were drafted and created along with Claude, then applied, run and checked commit by commit by me, including the manual tests above.

@manerlan manerlan requested a review from a team as a code owner November 16, 2026 22:49
@manerlan manerlan linked an issue Nov 16, 2026 that may be closed by this pull request
8 tasks
Copy link
Member

@williasc williasc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving. Tying reviews to accounts looks solid. The author always comes from the session and is never taken from input, myReview returns null when logged out instead of throwing, and the review_one_author check keeps the old anonymous reviews valid. The DB-backed API tests and the shared stubGraphQL helper for e2e are nice additions too.

A few things to look at, none of them blocking:

  • Expired session: if the session runs out, the frontend still shows the user as logged in. On the wine page the form shows up empty even if they have reviewed the wine, and saving fails. It would help to refetch Me when a request comes back UNAUTHENTICATED.
  • Login cache: in AuthProvider, writeQuery runs before resetStore(), which wipes it straight away. If the refetch fails, the user looks logged out. Calling clearStore() and then writeQuery avoids that.
  • Wrong error: the 23503 handler in saveReview now also catches the user_id foreign key, but the message always says "Wine not found".
  • Paging and cursors: in User.reviews, editing a review while paging can skip or repeat one, because the list is sorted by updated_at with offset cursors. A very large after cursor also gives a 500 instead of BAD_USER_INPUT.
  • Stuck form: in AuthForm, put setWorking(false) in a finally, so an error thrown by the submit handler can't lock the form.
  • Index: add id DESC to review_user_updated so it matches the ORDER BY.

These can go in a follow-up issue. Good work! 🍷

@williasc williasc merged commit d1034a9 into main Nov 16, 2026
@williasc williasc deleted the 65-tie-reviews-to-user-accounts branch November 16, 2026 22:54
Sign in to join this conversation on GitHub.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tie reviews to user accounts
2 participants